<template>
	<view class="login-container">
		<!-- 顶部图标区域 -->
		<view class="logo-box">
			<image class="logo-img" src="/static/logo.png" mode="aspectFit"></image>
		</view>

		<!-- 表单区域 -->
		<view class="form-box">
			<!-- 用户名输入框 -->
			<view class="input-box">
				<uni-icons type="person" size="20" color="#999"></uni-icons>
				<input type="text" v-model="username" placeholder="请输入用户名" placeholder-class="placeholder" />
			</view>

			<!-- 密码输入框 -->
			<view class="input-box">
				<uni-icons type="locked" size="20" color="#999"></uni-icons>
				<input type="password" v-model="password" placeholder="请输入密码" placeholder-class="placeholder" />
			</view>

			<!-- 登录按钮 -->
			<button class="login-btn" @click="handleLogin">登录</button>

			<!-- 其他登录选项 -->
			<view class="other-login-box">
				<view class="other-login-title">
					<view class="line"></view>
					<text>其他登录方式</text>
					<view class="line"></view>
				</view>

				<view class="icon-box">
					<view class="icon-item" @click="otherLoginType('weixin')">
						<uni-icons type="weixin" size="30" color="#07c160"></uni-icons>
						<text>微信</text>
					</view>
					<view class="icon-item" @click="otherLoginType('qq')">
						<uni-icons type="qq" size="30" color="#1296db"></uni-icons>
						<text>QQ</text>
					</view>
					<view class="icon-item" @click="otherLoginType('weibo')">
						<uni-icons type="weibo" size="30" color="#e6162d"></uni-icons>
						<text>微博</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 底部区域 -->
		<view class="bottom-box">
			<text class="register-text" @click="goToRegister">还没有账号？点击注册</text>
			<text class="forget-text" @click="forgetPassword">忘记密码</text>
		</view>
	</view>
</template>

<script>
	import { useCounterStore } from '@/stores/counter'
	export default {
		data() {
			return {
				username: '',
				password: '',
				counter:useCounterStore()
			}
		},
		methods: {
			// 处理登录
			handleLogin() {
				if (!this.username.trim()) {
					uni.showToast({
						title: '请输入用户名',
						icon: 'none'
					})
					return
				}

				if (!this.password.trim()) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					})
					return
				}

				// 模拟登录请求
				uni.showLoading({
					title: '登录中...'
				})


				uni.request({
					url: "http://47.94.6.186:3001/api/user/login",
					method: 'post',
					data: {
						loginname: this.username,
						password: this.password
					},
					success: (r) => {
						console.log(r.data)
						uni.setStorageSync("token", r.data.data.token);
						this.counter.useridFn(r.data.data.userid)
						uni.showToast({
							title: '登录成功',
							icon: 'success'
						})
						uni.hideLoading();
						uni.switchTab({
							url: '/pages/index/index'
						})
					}
				})

				// setTimeout(() => {
				// 	uni.hideLoading()

				// 	// 这里应该是实际的登录API调用
				// 	// 模拟登录成功
				// 	uni.showToast({
				// 		title: '登录成功',
				// 		icon: 'success'
				// 	})

				// 	// 登录成功后跳转到首页
				// 	setTimeout(() => {
				// 		uni.switchTab({
				// 			url: '/pages/index/index'
				// 		})
				// 	}, 1500)
				// }, 1000)
			},

			// 其他登录方式
			otherLoginType(type) {
				uni.showToast({
					title: `${type}登录功能开发中`,
					icon: 'none'
				})
			},

			// 跳转到注册页面
			goToRegister() {
				uni.navigateTo({
					url: '/pages/register/register'
				})
			},

			// 忘记密码
			forgetPassword() {
				uni.navigateTo({
					url: '/pages/forget/forget'
				})
			}
		}
	}
</script>

<style lang="scss">
	.login-container {
		display: flex;
		flex-direction: column;
		height: 100vh;
		padding: 0 50rpx;
		background-color: #f8f8f8;

		// 顶部图标区域
		.logo-box {
			display: flex;
			justify-content: center;
			margin-top: 100rpx;
			margin-bottom: 80rpx;

			.logo-img {
				width: 200rpx;
				height: 200rpx;
			}
		}

		// 表单区域
		.form-box {
			.input-box {
				display: flex;
				align-items: center;
				height: 100rpx;
				margin-bottom: 40rpx;
				padding: 0 30rpx;
				background-color: #ffffff;
				border-radius: 50rpx;
				box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.1);

				input {
					flex: 1;
					height: 100%;
					margin-left: 20rpx;
					font-size: 32rpx;
				}

				.placeholder {
					color: #c8c8c8;
				}
			}

			.login-btn {
				height: 100rpx;
				margin-top: 20rpx;
				background: linear-gradient(to right, #ff6b6b, #ff8e8e);
				color: #ffffff;
				border-radius: 50rpx;
				font-size: 36rpx;
				font-weight: bold;
				line-height: 100rpx;
				box-shadow: 0 5rpx 10rpx rgba(255, 107, 107, 0.3);
			}

			// 其他登录方式
			.other-login-box {
				margin-top: 80rpx;

				.other-login-title {
					display: flex;
					align-items: center;
					justify-content: center;
					margin-bottom: 50rpx;

					text {
						color: #999;
						font-size: 28rpx;
						margin: 0 20rpx;
					}

					.line {
						width: 100rpx;
						height: 1px;
						background-color: #e0e0e0;
					}
				}

				.icon-box {
					display: flex;
					justify-content: space-around;

					.icon-item {
						display: flex;
						flex-direction: column;
						align-items: center;

						text {
							margin-top: 10rpx;
							font-size: 24rpx;
							color: #666;
						}
					}
				}
			}
		}

		// 底部区域
		.bottom-box {
			display: flex;
			justify-content: space-between;
			margin-top: auto;
			margin-bottom: 50rpx;
			padding: 0 20rpx;

			.register-text,
			.forget-text {
				font-size: 28rpx;
				color: #666;
			}
		}
	}
</style>